﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Collections.Specialized;
using System.IO;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Media;
using System.Diagnostics;

namespace Rabbit.Utils
{
    public class Common
    {
        public static ListBoxItem GetListBoxItemFromChild(DependencyObject d)
        {
            if (VisualTreeHelper.GetParent(d).GetType() == typeof(ListBoxItem))
            {
                return VisualTreeHelper.GetParent(d) as ListBoxItem;
            }
            else
            {
                return GetListBoxItemFromChild(VisualTreeHelper.GetParent(d));
            }
        }
      
        public static void OpenUserPage(long userID)
        {
            OpenWebPage(
                string.Format("{0}{1}",
                Application.Current.Resources["UserBlogPrefix"].ToString(),
                userID));
        }

        public static void OpenWebPage(string url)
        {
            try
            {
                Process.Start(url);
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public static Dictionary<string, string> GetQueryAndValueFromString(string UriString)
        {
            int startIndex = UriString.IndexOf('?');
            if (startIndex == -1)
            {
                return null;
            }

            string queryString = UriString.Substring(startIndex+1);
            string[] queryArray = queryString.Split('&');
            if (queryArray.Length > 0)
            {
                Dictionary<string, string> dict = new Dictionary<string, string>();
                foreach (var query in queryArray)
                {
                    string key = query.Split('=')[0];
                    string value = query.Split('=')[1];
                    dict.Add(key, value);
                }

                return dict;
            }
            return null;
        }

        public static string EncryptString(string password)
        {
            StringBuilder  sbResult= new StringBuilder();
            for (int i = 1; i <= password.Length; i++)
            {
                int increased = i;
                char org = password[i - 1];
                char dest = i % 2 == 0 ? (char)(org + increased) : (char)(org - increased);
                sbResult.Append(dest);
            }
            return sbResult.ToString();
        }
        public static string DecryptString(string password)
        {
            StringBuilder sbResult = new StringBuilder();
            for (int i = 1; i <= password.Length; i++)
            {
                int increased = i; 
                char org = password[i-1];
                char dest = i % 2 == 0 ? (char)(org - increased) : (char)(org + increased);
                sbResult.Append(dest);
            }
            return sbResult.ToString();
        }
    }

    public static class Configuration
    {
        public static readonly int PageSize = 10;
        public static readonly double MaxMessageUpdateIntervalUserCanSet = 600;
        public static readonly double MinMessageUpdateIntervalUserCanSet = 30;
        public static readonly double MinMessageUpdateInterval = 10;
        public static readonly double MaxMessageUpdateInterval = 600;
    }
}
